<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('网盘文件列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>文件名：</label>
                            <input type="text" name="oldfilename"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <!--            <a class="btn btn-primary single disabled" onclick="$.operate.edit()"-->
            <!--               shiro:hasPermission="system:file:edit">-->
            <!--                <i class="fa fa-edit"></i> 修改-->
            <!--            </a>-->
            <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()"
               shiro:hasPermission="system:file:remove">
                <i class="fa fa-remove"></i> 删除
            </a>
            <a class="btn btn-warning multiple disabled" id="downloadall"
               shiro:hasPermission="system:file:downloadBatch">
                <i class="glyphicon glyphicon-download-alt"></i> 下载
            </a>

        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var download = [[${@permission.hasPermi('system:file:download')}]];
    var removeFlag = [[${@permission.hasPermi('system:file:remove')}]];
    var prefix = ctx + "cloud/file";

    $(function () {
        var options = {
            url: prefix + "/fileManagement/list",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            downloadUrl: prefix + "/downloadFile?fileId",
            modalName: "网盘文件",
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: '${comment}',
                    visible: false
                },

                {
                    field: 'oldfilename',
                    title: '文件名'
                },
                {
                    field: 'ext',
                    title: '后缀'
                },
                // {
                //     field: 'urlPath',
                //     title: '访问文件url地址'
                // },
                // {
                //     field: 'filePath',
                //     title: '文件存放路径'
                // },
                {
                    field: 'size',
                    title: '文件大小'
                },
                // {
                //     field: 'downcounts',
                //     title: '下载次数'
                // },
                {
                    field: 'uploadTime',
                    title: '上传时间'
                },
                // {
                //     field: 'isPublic',
                //     title: '是否共享'
                // },
                // {
                //     field: 'isCollect',
                //     title: '是否收藏'
                // },
                // {
                //     field: 'isShare',
                //     title: '是否分享'
                // },
                // {
                //     field: 'isRecycle',
                //     title: '是否在回收箱'
                // },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-warning btn-xs ' + download + '" href="javascript:void(0)" onclick="downloadFile(\'' + row.id + '\',\'' + row.oldfilename + '\')">下载<i class="fa fa-download"></i>');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    // 下载所有选择过的文件
    document.getElementById("downloadall").onclick = function () {
        debugger
        var rows = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一条记录");
            return;
        }
        const idListStr = Array.from(rows).toString();
        var dataParam = {"idListStr": idListStr};
        // window.location.href = "/cloud/file/downloadFileList?ids="+split;
        var f1 = $.ajax({
            url: prefix + '/downloadBatch',
            type: 'POST',
            data: dataParam,
            xhrFields: {
                responseType: 'blob'
            },
        })
        $.when(f1).then(function (content) {
            debugger
            var a = document.createElement('a')
            var blob = new Blob([content])
            var url = window.URL.createObjectURL(blob)
            a.href = url
            a.download = "文件批量下载.zip";
            a.click()
            window.URL.revokeObjectURL(url)
        }).catch((error) => {
            $.modal.alertError("文件下载失败");
        })
    };

    /**
     * 文件管理- 单文件下载
     * @param id
     * @param oldFileName
     */
    function downloadFile(id, oldFileName) {
        var dataParam = {"id": id};
        var url = prefix + "/downLoadOneFile";
        var f1 = $.ajax({
            url: url,
            type: 'POST',
            data: dataParam,
            xhrFields: {
                responseType: 'blob'
            },
        })
        $.when(f1).then(function (content) {
            debugger
            var a = document.createElement('a')
            var blob = new Blob([content])
            var url = window.URL.createObjectURL(blob)
            a.href = url
            a.download = oldFileName;
            a.click()
            window.URL.revokeObjectURL(url)
        }).catch((error) => {
            $.modal.alertError("文件下载失败");
        })
    }
</script>
</body>
</html>